package aaa.question.service.impl;

import java.io.IOException;
import java.util.List;

import aaa.question.domain.ExcelQuestion;
import aaa.question.domain.QuesOptionBank;
import aaa.question.mapper.OptionBank1Mapper;
import aaa.question.mapper.QuestionBank1Mapper;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import aaa.question.mapper.QuesQuestionBankMapper;
import aaa.question.domain.QuesQuestionBank;
import aaa.question.service.IQuesQuestionBankService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题目库Service业务层处理
 * 
 * @author xyy
 * @date 2025-01-06
 */
@Service
public class QuesQuestionBankServiceImpl implements IQuesQuestionBankService 
{
    @Autowired
    private QuesQuestionBankMapper quesQuestionBankMapper;

    @Autowired
    private QuestionBank1Mapper questionBank1Mapper;

    @Autowired
    private OptionBank1Mapper optionBank1Mapper;

    /**
     * 查询题目库
     * 
     * @param questionId 题目库主键
     * @return 题目库
     */
    @Override
    public QuesQuestionBank selectQuesQuestionBankByQuestionId(Long questionId)
    {
        return quesQuestionBankMapper.selectQuesQuestionBankByQuestionId(questionId);
    }

    /**
     * 查询题目库列表
     * 
     * @param quesQuestionBank 题目库
     * @return 题目库
     */
    @Override
    public List<QuesQuestionBank> selectQuesQuestionBankList(QuesQuestionBank quesQuestionBank)
    {
        return quesQuestionBankMapper.selectQuesQuestionBankList(quesQuestionBank);
    }

    /**
     * 新增题目库
     * 
     * @param quesQuestionBank 题目库
     * @return 结果
     */
    @Override
    public int insertQuesQuestionBank(QuesQuestionBank quesQuestionBank)
    {
        return quesQuestionBankMapper.insertQuesQuestionBank(quesQuestionBank);
    }

    /**
     * 修改题目库
     * 
     * @param quesQuestionBank 题目库
     * @return 结果
     */
    @Override
    public int updateQuesQuestionBank(QuesQuestionBank quesQuestionBank)
    {
        return quesQuestionBankMapper.updateQuesQuestionBank(quesQuestionBank);
    }

    /**
     * 批量删除题目库
     * 
     * @param questionIds 需要删除的题目库主键
     * @return 结果
     */
    @Override
    public int deleteQuesQuestionBankByQuestionIds(Long[] questionIds)
    {
        return quesQuestionBankMapper.deleteQuesQuestionBankByQuestionIds(questionIds);
    }

    /**
     * 删除题目库信息
     * 
     * @param questionId 题目库主键
     * @return 结果
     */
    @Override
    public int deleteQuesQuestionBankByQuestionId(Long questionId)
    {
        return quesQuestionBankMapper.deleteQuesQuestionBankByQuestionId(questionId);
    }

    @Override
    public Object importdate(MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<ExcelQuestion> quesQuestionBanks =reader.readAll(ExcelQuestion.class);
        int i = 0;
        for (ExcelQuestion question : quesQuestionBanks) {
            System.out.println(question);
            switch (question.getQuestionType())
            {
                case "java":
                    int javaId =questionBank1Mapper.findMaxJavaQuestionNumByQuestionType(question.getQuestionType())+1;
                    i= i + questionBank1Mapper.insertQuestionJava(question.getQuestionText(),question.getQuestionType(),question.getCorrectAnswer(),javaId);
                    break;
                case "c":
                    int cId =questionBank1Mapper.findMaxCQuestionNumByQuestionType(question.getQuestionType())+1;
                    i= i + questionBank1Mapper.insertQuestionC(question.getQuestionText(),question.getQuestionType(),question.getCorrectAnswer(),cId);
                    break;
                case "computer":
                    int computerId =questionBank1Mapper.findMaxComputerQuestionNumByQuestionType(question.getQuestionType())+1;
                    i= i + questionBank1Mapper.insertQuestionComputer(question.getQuestionText(),question.getQuestionType(),question.getCorrectAnswer(),computerId);
                    break;
                default:
                    throw new RuntimeException("Unsupported question type: " + question.getQuestionType());
            }

            optionBank1Mapper.insertByDTO(questionBank1Mapper.findIdByQuestionText(question.getQuestionText()), question.getOptionCharA(), question.getOptionTextA());
            optionBank1Mapper.insertByDTO(questionBank1Mapper.findIdByQuestionText(question.getQuestionText()), question.getOptionCharB(), question.getOptionTextB());
            optionBank1Mapper.insertByDTO(questionBank1Mapper.findIdByQuestionText(question.getQuestionText()), question.getOptionCharC(), question.getOptionTextC());
            optionBank1Mapper.insertByDTO(questionBank1Mapper.findIdByQuestionText(question.getQuestionText()), question.getOptionCharD(), question.getOptionTextD());

        }

        return i;
    }
}
